package com.airn.siege.lion.custom.widget

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.airn.siege.lion.custom.widget.databinding.ActivityMainBinding
import com.airn.siege.lion.lib.widget.ViewManager4Selector
import java.util.*

class MainActivity : AppCompatActivity() {

    companion object {
        private val TAG: String = MainActivity::class.java.simpleName
    }

    private lateinit var binding: ActivityMainBinding

    private var viewManager4Selector: ViewManager4Selector? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        Log.i(TAG, "onCreate")

        viewManager4Selector = ViewManager4Selector()
        viewManager4Selector?.addView(binding.tvSelect1, binding.tvSelect2)
        viewManager4Selector?.selectView(binding.tvSelect1)
        refreshData()
        binding.tvSelect1.setOnClickListener {
            clickSelect(it)
        }

        binding.tvSelect2.setOnClickListener {
            clickSelect(it)
        }

        binding.llSelectTime.setOnClickListener {
            DateSelectorUtils.dateSelector(
                this@MainActivity,
                defaultTimeStr = binding.tvTime.text?.toString(),
                showSplit = true,
            ) { timeStr, timeDate ->
                binding.tvTime.text = timeStr
            }
        }

        binding.llSelectTimeD.setOnClickListener {
            DateSelectorUtils.dateChangeCNPDSelector(
                this@MainActivity,
                defaultTimeStr = binding.tvTimeD.text?.toString(),
                minDate =  Date(),
                maxDate = Date(Date().time + 14 * 1000 * 60 * 60 * 24),
                showSplit = true,
            ) { timeStr, timeDate ->
                binding.tvTimeD.text = timeStr
            }
        }

        binding.llSelectTimeChangeVer.setOnClickListener {
            DateSelectorUtils.dateChangeVerSelector(
                this@MainActivity, defaultTimeStr = binding.tvTimeChangeVer.text?.toString(), showSplit = true
            ) { timeStr, timeDate ->
                binding.tvTimeChangeVer.text = timeStr
            }
        }

        binding.btnSelect.setOnClickListener {
            startActivity(Intent(this@MainActivity,ScrollPickerActivity::class.java))
        }
    }

    private fun clickSelect(v: View) {
        val selectView = viewManager4Selector?.selectView
        if (v === selectView) return
        viewManager4Selector?.selectView(v)
        refreshData() // 刷新数据
    }

    private fun refreshData() {
        if (viewManager4Selector == null) return
        val selectView = viewManager4Selector?.selectView ?: return

        val tag = selectView.tag.toString()
        if ("tab1" == tag) { //
            binding.tvStatus.text = getString(R.string.tips_current_select,"tab1")
        } else if ("tab2" == tag) { //
            binding.tvStatus.text = getString(R.string.tips_current_select,"tab2")
        }
    }
}